package parser;

import java.io.IOException;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;

public class WordsExtractorFactory
{
	private static final WordsExtractorFactory instance =  new WordsExtractorFactory();
	
	private WordsExtractorFactory()
	{
		
	}
	
	public static WordsExtractorFactory getInstance()
	{
		return instance;
	}
	
	public WordsExtractor creatWordsExtractor(Multipart multipart)
	{
		return new MultipartWordsExtractor(multipart);
	}
	
	public WordsExtractor creatWordsExtractor(Part part)
	{
		WordsExtractor wordsExtractor;
		MimeType mimeType;
		try
		{
			mimeType = new MimeType(part.getContentType());
//			System.out.println("MIME Type : " + mimeType.getBaseType());
			switch (mimeType.getBaseType())
			{
			case "text/plain":
				wordsExtractor = new TextWordExtractor(part);
				break;
				
			case "text/html":
				wordsExtractor = new HTMLWordsExtractor(part);
				break;
				
			case "multipart/mixed":
			case "multipart/signed":
			case "multipart/related":
			case "multipart/alternative":
				wordsExtractor = new MultipartWordsExtractor((Multipart) part.getContent());
				break;
				
			default:
				wordsExtractor = null;
				break;
			}
		}
		catch (MimeTypeParseException | MessagingException | IOException e)
		{
			e.printStackTrace();
			wordsExtractor = null;
		}
		return wordsExtractor;
	}
}
